using System.Collections;
using System.Collections.Generic;
using ProjectSurvival.System.Save;
using QFramework;
using UnityEngine;

namespace ProjectSurvival
{
    public class Global : Architecture<Global>
    {
        public static BindableProperty<int> Experience = new BindableProperty<int>();
        public static BindableProperty<int> Level = new BindableProperty<int>(1);
        public static BindableProperty<float> CurrentTime = new BindableProperty<float>(0);
        public static BindableProperty<int> Coin = new BindableProperty<int>(0);
        public static BindableProperty<float> ExpPercent = new BindableProperty<float>(0.4f);
        public static BindableProperty<float> CoinPercent = new BindableProperty<float>(0.1f);
        public static BindableProperty<int> MaxHp = new BindableProperty<int>(3);
        public static BindableProperty<int> Hp = new BindableProperty<int>(3);

        //简单剑
        public static BindableProperty<bool> SimpleSwordUnlocked = new BindableProperty<bool>(false);
        public static BindableProperty<float> AbilityDamage = new BindableProperty<float>(Config.InitSimpleSwordDamage);

        public static BindableProperty<float> AttackDuration =
            new BindableProperty<float>(Config.InitSimpleSwordDuration);

        public static BindableProperty<int> SimpleSwordCount = new BindableProperty<int>(Config.InitSimpleSwordCount);

        public static BindableProperty<float> SimpleSwordRange =
            new BindableProperty<float>(Config.InitSimpleSwordRange);

        //飞刀
        public static BindableProperty<bool> SimpleKnifeUnlocked = new BindableProperty<bool>(false);

        public static BindableProperty<float> SimpleKnifeDamage =
            new BindableProperty<float>(Config.InitSimpleKnifeDamage);

        public static BindableProperty<float> SimpleKnifeDuration =
            new BindableProperty<float>(Config.InitSimpleKnifeDuration);

        public static BindableProperty<int> SimpleKnifeCount = new BindableProperty<int>(Config.InitSimpleKnifeCount);

        public static BindableProperty<int> SimpleKnifeAttackCount =
            new BindableProperty<int>(Config.InitSimpleKnifeAttackCount);

        //守护剑
        public static BindableProperty<bool> SimpleRotateSworUnlocked = new BindableProperty<bool>(false);

        public static BindableProperty<float> SimpleRotateSwordDamage =
            new BindableProperty<float>(Config.InitSimpleRotateSwordDamage);

        public static BindableProperty<int> SimpleRotateSwordCount =
            new BindableProperty<int>(Config.InitSimpleRotateSwordCount);

        public static BindableProperty<float> SimpleRotateSwordSpeed =
            new BindableProperty<float>(Config.InitSimpleRotateSwordSpeed);

        public static BindableProperty<float> SimpleRotateSwordRange =
            new BindableProperty<float>(Config.InitSimpleRotateSwordRange);

        //篮球
        public static BindableProperty<bool> SimpleBasketBallUnlocked = new BindableProperty<bool>(false);

        public static BindableProperty<float> SimpleBasketBallDamage =
            new BindableProperty<float>(Config.InitSimpleBasketBallDamage);

        public static BindableProperty<float> SimpleBasketBallSpeed =
            new BindableProperty<float>(Config.InitSimpleBasketBallSpeed);

        public static BindableProperty<int> SimpleBasketBallCount =
            new BindableProperty<int>(Config.InitSimpleBasketBallCount);

        //炸弹
        public static BindableProperty<bool> SimpleBombUnlocked = new BindableProperty<bool>(false);

        public static BindableProperty<float> SimpleBombDamage =
            new BindableProperty<float>(Config.InitSimpleBombDamage);

        public static BindableProperty<float> SimpleBombPercent =
            new BindableProperty<float>(Config.InitSimpleBombPercent);

        public static BindableProperty<float> CriticalRate = new BindableProperty<float>(Config.InitCriticalRate);
        public static BindableProperty<float> DamageRate = new BindableProperty<float>(1);
        public static BindableProperty<int> AdditionalFlyThingCount = new BindableProperty<int>(0);

        public static BindableProperty<float> MovementSpeedRate =
            new BindableProperty<float>(Config.InitMovementSpeedRate);

        public static BindableProperty<float> CollectableArea = new BindableProperty<float>(Config.InitCollectableArea);

        public static BindableProperty<float> AdditionalExpPercent = new BindableProperty<float>(0);

        public static BindableProperty<bool> SuperKnife = new BindableProperty<bool>(false);
        public static BindableProperty<bool> SuperSword = new BindableProperty<bool>(false);
        public static BindableProperty<bool> SuperRotateSword = new BindableProperty<bool>(false);
        public static BindableProperty<bool> SuperBomb = new BindableProperty<bool>(false);
        public static BindableProperty<bool> SuperBasketBall = new BindableProperty<bool>(false);

        // public static EasyEvent OnInitFinish = new EasyEvent();
        public static void ResetData()
        {
            Hp.Value = MaxHp.Value;
            Experience.Value = 0;
            Level.Value = 1;
            AbilityDamage.Value = 1;
            AttackDuration.Value = 1.5f;
            CurrentTime.Value = 0;

            SimpleSwordUnlocked.Value = false;
            AbilityDamage.Value = Config.InitSimpleSwordDamage;
            AttackDuration.Value = Config.InitSimpleSwordDuration;
            SimpleSwordCount.Value = Config.InitSimpleSwordCount;
            SimpleSwordRange.Value = Config.InitSimpleSwordRange;

            SimpleKnifeUnlocked.Value = false;
            SimpleKnifeDamage.Value = Config.InitSimpleKnifeDamage;
            SimpleKnifeDuration.Value = Config.InitSimpleKnifeDuration;
            SimpleKnifeCount.Value = Config.InitSimpleKnifeCount;
            SimpleKnifeAttackCount.Value = Config.InitSimpleKnifeAttackCount;

            SimpleRotateSworUnlocked.Value = false;
            SimpleRotateSwordDamage.Value = Config.InitSimpleRotateSwordDamage;
            SimpleRotateSwordCount.Value = Config.InitSimpleRotateSwordCount;
            SimpleRotateSwordSpeed.Value = Config.InitSimpleRotateSwordSpeed;
            SimpleRotateSwordRange.Value = Config.InitSimpleRotateSwordRange;

            SimpleBasketBallUnlocked.Value = false;
            SimpleBasketBallDamage.Value = Config.InitSimpleBasketBallDamage;
            SimpleBasketBallSpeed.Value = Config.InitSimpleBasketBallSpeed;
            SimpleBasketBallCount.Value = Config.InitSimpleBasketBallCount;

            SimpleBombUnlocked.Value = false;
            SimpleBombDamage.Value = Config.InitSimpleBombDamage;
            SimpleBombPercent.Value = Config.InitSimpleBombPercent;

            CriticalRate.Value = Config.InitCriticalRate;
            DamageRate.Value = 1;
            AdditionalFlyThingCount.Value = 0;

            MovementSpeedRate.Value = Config.InitMovementSpeedRate;
            CollectableArea.Value = Config.InitCollectableArea;
            AdditionalExpPercent.Value = 0;

            SuperKnife.Value = false; 
            SuperSword.Value = false; 
            SuperRotateSword.Value = false; 
            SuperBomb.Value = false; 
            SuperBasketBall.Value = false; 

            PlayerPrefs.GetInt(nameof(Coin), 0);
            //EnemyGenerator.EnemyCount.Value = 0;

            Interface.GetSystem<LevelUpSystem>().ResetData();
        }

        [RuntimeInitializeOnLoadMethod]
        public static void AutoInitialize()
        {
            AudioKit.PlaySoundMode = AudioKit.PlaySoundModes.IgnoreSameSoundInSoundFrames;
            //先读取数据
            Coin.Value = PlayerPrefs.GetInt(nameof(Coin), 0);
            ExpPercent.Value = PlayerPrefs.GetFloat(nameof(ExpPercent), 0.4f);
            CoinPercent.Value = PlayerPrefs.GetFloat(nameof(CoinPercent), 0.1f);
            MaxHp.Value = PlayerPrefs.GetInt(nameof(MaxHp), 3);
            Hp.Value = MaxHp.Value;

            //注册变更事件，数据有变化后永久储存
            Coin.Register((coinCount) => { PlayerPrefs.SetInt(nameof(Coin), coinCount); });
            ExpPercent.Register((expPercent) => { PlayerPrefs.SetFloat(nameof(ExpPercent), expPercent); });
            CoinPercent.Register((coinPercent) => { PlayerPrefs.SetFloat(nameof(CoinPercent), coinPercent); });
            MaxHp.Register((maxHp) => { PlayerPrefs.SetInt(nameof(MaxHp), maxHp); });
            // ResKit.InitAsync().ToAction().StartGlobal(() =>
            // {
            //     
            //     // OnInitFinish.Trigger();
            // });
            //ResKit.Init();  
            //UIKit.Root.SetResolution(1920, 1080, 1);

            
        }

        public static int LevelUpToNextExperience()
        {
            return 5 * Global.Level.Value;
        }

        public static void GeneratePowerUp(Transform parent, bool genTreasureChest)
        {
            if (genTreasureChest)
            {
                GeneratePowerUpManager.Instance.Treasure
                    .Instantiate()
                    .Position(parent.position)
                    .Show();
                return; 
            }

            //生成经验球
            var randomValue = Random.Range(0, 1f);
            if (randomValue <= ExpPercent.Value + AdditionalExpPercent.Value)
            {
                GeneratePowerUpManager.Instance.Exp.Instantiate()
                    .Position(parent.position)
                    .Show();
                return;
            }

            //生成金币
            randomValue = Random.Range(0, 1f);
            if (randomValue <= CoinPercent.Value)
            {
                GeneratePowerUpManager.Instance.Coin.Instantiate()
                    .Position(parent.position)
                    .Show();
                return;
            }

            //生成hp
            randomValue = Random.Range(0, 1f);
            if (randomValue <= 0.1f&& !Object.FindObjectOfType<MyHp>())
            {
                GeneratePowerUpManager.Instance.MyHp.Instantiate()
                    .Position(parent.position)
                    .Show();
                return;
            }

            //生成炸弹
            if (Global.SimpleBombUnlocked.Value && Object.FindObjectsOfType<Bomb>().Length == 0)
            {
                randomValue = Random.Range(0, 1f);
                if (randomValue <= SimpleBombPercent.Value)
                {
                    GeneratePowerUpManager.Instance.Bomb.Instantiate()
                        .Position(parent.position)
                        .Show();
                    return;
                }
            }

            //生成拾取经验道具
            randomValue = Random.Range(0, 1f);
            if (randomValue <= 0.1f&& !Object.FindObjectOfType<PickAllExp>())
            {
                GeneratePowerUpManager.Instance.PickAllExp.Instantiate()
                    .Position(parent.position)
                    .Show();
                return;
            }
        }

        protected override void Init()
        {
            RegisterSystem(new SaveSystem());
            RegisterSystem(new CoinUpgradeSystem());
            RegisterSystem(new LevelUpSystem());
            RegisterSystem(new AchievementSystem());
        }
    }
}